package com.yida.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.model.RoutingRule.Redirect;
import com.yida.bean.blogUser;
import com.yida.service.serveruser;

@Controller
public class user {

	@Autowired
	serveruser serveruser;
	
		@RequestMapping("/user")
		public String blogroll(Integer id,Model model,HttpSession session) {
			blogUser sessionUser;
			
			Object sessionUserObject =   session.getAttribute("user");
			if (sessionUserObject == null) {
				return   "redirect:/blog/index";
			}else {
				 sessionUser = (blogUser) sessionUserObject;
				 if (sessionUser.getId()!=null) {
					 blogUser user = serveruser.selectuser(id);
				     model.addAttribute("user", user);
				     return "user";
				}else {
				   return  "redirect:/blog/index";
				}
			}
			

		}
		
		@PostMapping("/user/img/up")
		@ResponseBody
		public Object imgup(Integer id,MultipartFile file,HttpSession session) {	
			String result =    serveruser.fileup(id, file,session);
			Map<String, String>  res = new HashMap<String, String>();
			
			
			if (!"false".equals(res)) {
				res.put("result", "true");
				res.put("imgurl", result);
			}else {
				 res.put("result", "false");
			}
			
			return res;
		}
		
		
		@PostMapping("/user/img/url")
		@ResponseBody
		public Object urlup(Integer id,String url,HttpSession session) {
			
			boolean result = serveruser.imgUpdate(id, url,session);
			Map<String, String> map = new HashMap<String, String>();
			
			if (result) {
				map.put("result", "true");
			}else {
				map.put("result", "false");
			}
			
			return map;
			
			
		}
		
		@PostMapping("/user/yzm/email")
		@ResponseBody
		public Object email(Integer id, String email) {
			
			boolean result = serveruser.emailyzm(id,email);
			
			Map<String, String> map = new HashMap<String, String>();
			
			if (result) {
				map.put("result", "true");
			}else {
				map.put("result", "false");
			}
			
			return map;

		}
		
		@PostMapping("/user/yzm/email/yz")
		@ResponseBody
		public Object emailyz(Integer id, String email,String emailcode) {
			System.out.println(id+","+email+","+emailcode);
			
			boolean result = serveruser.emailyzmyz(id,email,emailcode);
			
			Map<String, String> map = new HashMap<String, String>();
			
			if (result) {
				map.put("result", "true");
			}else {
				map.put("result", "false");
			}
			
			return map;

		}
		
		@PostMapping("/user/pwdemail")
		@ResponseBody
		public Object pwdemail(Integer id) {
			
			blogUser user = serveruser.selectuser(id);
			
			Map<String, String> map = new HashMap<String, String>();
			
			//放置密码为不为空
			if (user.getPassword()!=null) {
				map.put("pwd", "true");
			}else {
				map.put("pwd", "false");
			}
			
			
			
			return map;

		}
		
		@PostMapping ("/user/all")
		public String userAll(blogUser user) {

			Integer result = serveruser.updateUserAll(user);
			
			Map<String, String> map = new HashMap<String, String>();
				
			return  "redirect:/user?id="+user.getId() ;
			

		}
		

}
